﻿//var $ = function(id) {return document.getElementById(id);};
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
function MouseOverBtn(){event.srcElement.className+="Hover";}
function MouseOutBtn() {event.srcElement.className=event.srcElement.className.substr(0,event.srcElement.className.indexOf("Hover"));}
function CorrectButton()
{
   var inputs=document.getElementsByTagName("INPUT");
   for(var i=0; i<inputs.length; i++)
   {
      var el = inputs[i];
      var elType = el.type.toLowerCase();
      var elClass = el.className.toLowerCase();
      var elLength = Math.ceil(el.value.replace(/[^\x00-\xff]/g,"**").length/2);
      if(elType!="button" && elType!="submit" && elType!="reset" || elClass!="bigbutton"&&elClass!="smallbutton")
         continue;
      
      if(elLength<=3)
         el.className+="A";
      else if(elLength==4)
         el.className+="B";
      else if(elLength>=5 && elLength<=7)
         el.className+="C";
      else if(elLength>=8 && elLength<=11)
         el.className+="D";
      else
         el.className+="E";
      
      if(is_ie)
      {
         el.attachEvent("onmouseover", MouseOverBtn);
         el.attachEvent("onmouseout",  MouseOutBtn);
      }
   }
}

function isInteger(str){
    return IsValid(str, /^\d+$/);
}
function isNum(str){
    return IsValid(str, /^\d+(\.\d+)?$/);
}
function IsNumber(str)
{
   return str.match(/^[0-9]*$/)!=null;
}
function IsTelephone(str)
{
   //if(str.length == 0) return true;
   //return str.match(/^0?1[35]\d{9}|(0{1,2}\d{2,3}-?)?\d{7}\d?(-\d+)?$/)!=null;
//    if(str.length == 0) return true;
//	if (str.match(/^0?1[35]\d{9}$/) == null)
//	{
//		return str.match(/^(0{1,2}\d{2,3}-?)?\d{7}\d?(-\d+)?$/)!=null;
//	}

    return IsValid(str, /^1[358]\d{9}|(0{1,2}\d{2,3}-?)?\d{7}\d?(-\d+)?$/);
}

function IsEmail(str)
{
   if(str.length == 0) return true;
   return IsValid(str, /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
}

function notEmpty(element)
{
    if(element && element.tagName === "INPUT")
    {
        return element.value.length > 0;
    }
}
function IsValid(val, regex) {
    //if (ValidatorTrim(val).length == 0)
    //    return true;
    if (val.length == 0) return true;
    var rx = new RegExp(regex);
    var matches = rx.exec(val);
    return (matches != null && val == matches[0]);
}

function ValidatorTrim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

if(is_ie)
   window.attachEvent("onload", CorrectButton);
else
   window.addEventListener("load", CorrectButton,false);
   
/* *********  胡世波2005-01-15添加*************** */
String.prototype.Trim = function()
{
   return this.replace(/(^\s*)|(\s*$)/g, "");
}

function validateCNDate(strValue) {
    strValue = strValue.Trim();
    if (strValue == "") return true;
    if (strValue.length > 10)
        strValue = strValue.substr(0, strValue.indexOf(" "));
    //alert(strValue);
    var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/

    if (!objRegExp.test(strValue)) {
        //alert(objRegExp.test(strValue));
        return false;
    }
    else {
        var arrayDate = strValue.split(RegExp.$1);
        var intYear = parseInt(arrayDate[0], 10);
        var intMonth = parseInt(arrayDate[1], 10);
        var intDay = parseInt(arrayDate[2], 10);

        if (intMonth > 12 || intMonth < 1) {
            return false;
        }

        var arrayLookup = { '1': 31, '3': 31, '4': 30, '5': 31, '6': 30, '7': 31,
            '8': 31, '9': 30, '10': 31, '11': 30, '12': 31
        }

        if (arrayLookup[intMonth] != null) {
            if (intDay <= arrayLookup[intMonth] && intDay != 0)
                return true;
        }

        if (intMonth - 2 == 0) {
            var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
            if (((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <= 28)) && intDay != 0)
                return true;
        }
    }
    return false;
}
        
function isDate(element, str)
{
    var retval = true;
    if(typeof(str) !== "undefined" && str !== null)
    {
        str = str.Trim();
        element.value = str;
    }
  
    if(element.value.length > 0 && !validateCNDate(element.value))
    {
        element.value = "";
        retval = false;
    }
    return retval;
}
/* ****************************************************** */  
function CheckAll(me)
{
    var index = me.name.indexOf('CheckBoxButton');  
    var prefix = me.name.substr(0,index);
    if( prefix.charAt(prefix.length - 1) == "$")
	{
		prefix = prefix.slice(0, prefix.length - 2);
		prefix = prefix.substr( 0, prefix.lastIndexOf("$") );
	}
    for(i=0; i<document.forms[0].length; i++) 
    { 
        var o = document.forms[0][i]; 
        if (o.type == 'checkbox') 
        { 
            if (me.name != o.name) 
            {
                if (o.name.substring(0, prefix.length) == prefix && o.name.indexOf('CheckBoxButton') > 0 ) 
                {
                    // Must be this way
                    o.checked = !me.checked; 
                    o.click(); 
                }
            }
        } 
    } 
}
			function moveinfo()
			{
				t_info2.style.visibility="visible";
				t_info2.style.filter="Alpha(opacity=70)"
				t_info2.style.left=document.body.scrollLeft+window.event.clientX-20;
				t_info2.style.top=document.body.scrollTop+window.event.clientY+7;
			}
			function showinfo(obj,adline)
			{
				t_info2.innerHTML="<b><font color=red>"+adline+"</font></b>";
			}
			function clearinfo()
			{
				t_info2.style.filter="";
				t_info2.style.visibility="hidden";
			}
